#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/5/22 17:06
# @Author  : Leixu
# @Site    : 
# @File    : url_feeder.py
# @Software: PyCharm Community Edition


import uuid
from hashlib import md5


class UrlFeeder(object):
    def __init__(self, launcher):
        self.launcher = launcher

    def add(self, url_single_or_list, callback, if_need_cache=False):
        if isinstance(url_single_or_list, str):
            self.load_to_launcher(url_single_or_list, callback, if_need_cache)
        elif isinstance(url_single_or_list, list):
            for i in url_single_or_list:
                self.load_to_launcher(i, callback, if_need_cache)

    def load_to_launcher(self, url, callback, if_need_cache):
        m = md5()
        m.update(bytes(url, encoding="utf8"))
        url_md5 = m.hexdigest()
        url_uuid = uuid.uuid1()
        self.launcher.load_url({"url": url, "callback": callback, "uuid": url_uuid, "url_md5": url_md5})


class CrawlerFeeder(object):
    def __init__(self, launcher):
        self.launcher = launcher

    def add(self, crawler, callback):
        self.load_to_launcher(crawler, callback)

    def load_to_launcher(self, crawler, callback):
        self.launcher.load_crawler({"crawler": crawler, "callback": callback})
